package rtu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import GUI.equipmentGUI;

import mdrtu.re_1;
import mdrtu.re_6;
import dao.MoistureDao;
import dao.modelDao;
import dao.senseDeviceDao;
import entity.ConnectionInfor;
import entity.Hatchway;
import entity.Model;
import entity.Moisture;
import entity.SenseDevice;

/*
 * �豸����
 * */
public class rtu_Classify {
	private static int com_nu11mber,register_address,model_address,device_type;
	private static String com_number;
	public static ArrayList<SenseDevice> senseDeviceInfor;
	public static ArrayList<SenseDevice> sensedevice3;
	public static ArrayList<SenseDevice> sensedevice6;
	public static ArrayList<SenseDevice> sensedevice1;
	public static ArrayList<Model> model;
	private static int[][] address;
	private static int [] arry;
	Hatchway hatchway;
	ConnectionInfor connectioninfor= new ConnectionInfor();
	private Timer timer;
	
	public rtu_Classify(Hatchway hatchway,ArrayList<Model>  modelall ,ArrayList<SenseDevice> senseDevice ){
		this.hatchway =  hatchway;
		 model=modelDao.Read_modelAdress(hatchway, modelall);
		sensedevice3 =  senseDeviceDao.Read_Register_type3(senseDevice);
		sensedevice1 =  senseDeviceDao.Read_Register_type1(senseDevice);
		sensedevice6 =  senseDeviceDao.Read_Register_type6(senseDevice);
		timer = new Timer();
		timer.schedule(new sendtask(), 0, 2000);
		}
	
	int[] mac_arr( ArrayList<SenseDevice> M_sensedevice5) {
	
		int n =M_sensedevice5.size();
		int[] mac = new int[n];
		for (int j = 0; j < n; j++) {
			mac[j] = Integer.parseInt(M_sensedevice5.get(j).getRegister_address());
		}
		Arrays.sort(mac);//��������
		return mac;
	}

	private boolean consistent(int[] mac) {

		boolean to = false;
		int first = mac[0];
		for (int i = 0; i < mac.length; i++) {
			if (first == mac[i]) {
				first++;
				to = true;
			} else {
				to = false;
				break;
			}
		}
		return to;
	}
	
	class sendtask extends TimerTask {
		public void run() {
			
				if (sensedevice1.size() != 0){
					for(int i = 0; i<model.size(); i++){
					int	modeladdress = model.get(i).getModel_address();
					int	modelid = Integer.parseInt( model.get(i).getId());
					 ArrayList<SenseDevice> M_sensedevice1 = senseDeviceDao.Read_Register_address(modelid, sensedevice1);//��Ӧ�ڵ��µ�����Do���͵ļĴ���
					 if (M_sensedevice1.size() != 0){
					 int mac_arr[] = mac_arr(M_sensedevice1);
					 if (consistent(mac_arr)) {
						 connectioninfor.setBaud_rate(hatchway.getBaud_rate());
						 connectioninfor.setData_bits(hatchway.getData_bits());
						 connectioninfor.setModel_address(modeladdress);
						 connectioninfor.setOdd_even_check(hatchway.getOdd_even_check());
						 connectioninfor.setRegister_address(mac_arr[0]);
						 connectioninfor.setRegister_number(mac_arr.length);
						 connectioninfor.setStop_bit(hatchway.getStop_bit());
						 connectioninfor.setStream_control(hatchway.getStream_control());
						 connectioninfor.setHatchway_name(hatchway.getHatchway_name());
						 re_1 A = new re_1(connectioninfor);
						int[] senserdata =  A.remain1();
						 register_address= mac_arr[0];
						 //����ȡ����ݴ�����ݿ�
						 for(int i1=0;i1<senserdata.length;i1++){
							 Moisture moisture=new Moisture();
							 String device_id=senseDeviceDao.Read_device_id(modelid, register_address+i1);
							 String value=String.valueOf(senserdata[i1]);
							 moisture.setDevice_id(device_id);
							 moisture.setValue(value);
							 MoistureDao.insert(moisture);
							 System.out.println("register_address:"+register_address+i1+"  "+"device_id: "+device_id+"  "+"senserdata: "+senserdata[i1]);
						 }
					 }else{
						 for(int j = 0; j<mac_arr.length; j++){
							 connectioninfor.setBaud_rate(hatchway.getBaud_rate());
							 connectioninfor.setData_bits(hatchway.getData_bits());
							 connectioninfor.setModel_address(modeladdress);
							 connectioninfor.setOdd_even_check(hatchway.getOdd_even_check());
							 connectioninfor.setRegister_address(mac_arr[j]);
							 connectioninfor.setRegister_number(1);
							 connectioninfor.setStop_bit(hatchway.getStop_bit());
							 connectioninfor.setStream_control(hatchway.getStream_control());
							 connectioninfor.setHatchway_name(hatchway.getHatchway_name());
							 re_1 A = new re_1(connectioninfor);
								int[] senserdata =  A.remain1();						
								 register_address= mac_arr[j];
								 String device_id=senseDeviceDao.Read_device_id(modelid, register_address);
								 //����ȡ����ݴ�����ݿ�
								 for(int i1=0;i1<senserdata.length;i1++){
									 Moisture moisture=new Moisture();
									 String value=String.valueOf(senserdata[i1]);
									 moisture.setDevice_id(device_id);
									 moisture.setValue(value);
									 MoistureDao.insert(moisture);
									 System.out.println("register_address:"+register_address+"  "+"device_id: "+device_id+"  "+"senserdata: "+senserdata[i1]);
								 }
						 		}
					 		}
					 	}
					}
				}
				
				if (sensedevice3.size() != 0){
					for(int i = 0; i<model.size(); i++){
					int	modeladdress = model.get(i).getModel_address();//�ڵ��ַ
					int	modelid = Integer.parseInt( model.get(i).getId());//�ڵ�id
					 ArrayList<SenseDevice> M_sensedevice3 = senseDeviceDao.Read_Register_address(modelid, sensedevice3);//��Ӧ�ڵ��µ�����Do���͵ļĴ���
					if (M_sensedevice3.size() != 0){
						arry = mac_arr(M_sensedevice3);
					 if (consistent(arry)) {
						 connectioninfor.setBaud_rate(hatchway.getBaud_rate());
						 connectioninfor.setData_bits(hatchway.getData_bits());
						 connectioninfor.setModel_address(modeladdress);
						 connectioninfor.setOdd_even_check(hatchway.getOdd_even_check());
						 connectioninfor.setRegister_address(arry[0]);
						 connectioninfor.setRegister_number(arry.length);
						 connectioninfor.setStop_bit(hatchway.getStop_bit());
						 connectioninfor.setStream_control(hatchway.getStream_control());
						 connectioninfor.setHatchway_name(hatchway.getHatchway_name());//���ں�
						 System.out.println("串口号：" + hatchway.getHatchway_name()+"    寄存器长度："+arry.length);
						 re_3 B = new re_3();
//						 while(true){
//							 int[] senserdata = B.remain1(connectioninfor) ;
//						 }
						 int[] senserdata = B.remain1(connectioninfor) ;			 
						 register_address= arry[0];
						 for(int i1=0;i1<senserdata.length;i1++){
							 Moisture moisture=new Moisture();
							 String device_id=senseDeviceDao.Read_device_id(modelid, register_address+i1);
							 String value=String.valueOf(senserdata[i1]);
							 moisture.setDevice_id(device_id);
							 moisture.setValue(value);
							 MoistureDao.insert(moisture);
							 System.out.println("register_address:"+register_address+i1+"  "+"device_id: "+device_id+"  "+"senserdata: "+senserdata[i1]);
							// equipmentGUI.addTable_Moisture(moisture) ;
						 }
						 			 
//						 System.out.println("modeladdress:"+modeladdress+"    返回数据："+Arrays.toString(senserdata));

					 }else{
						 for(int j = 0; j<arry.length; j++){
			//				 ConnectionInfor connectioninfor= new ConnectionInfor();
							 connectioninfor.setBaud_rate(hatchway.getBaud_rate());
							 connectioninfor.setData_bits(hatchway.getData_bits());
							 connectioninfor.setModel_address(modeladdress);
							 connectioninfor.setOdd_even_check(hatchway.getOdd_even_check());
							 connectioninfor.setRegister_address(arry[j]);
							 connectioninfor.setRegister_number(1);
							 connectioninfor.setStop_bit(hatchway.getStop_bit());
							 connectioninfor.setStream_control(hatchway.getStream_control());
							 connectioninfor.setHatchway_name(hatchway.getHatchway_name());//���ں�
							 re_3 B = new re_3();
							 int[] senserdata = B.remain1(connectioninfor) ;							 
							 register_address= arry[j];
							 String device_id=senseDeviceDao.Read_device_id(modelid, register_address);
							 for(int i1=0;i1<senserdata.length;i1++){
								 Moisture moisture=new Moisture();
								 String value=String.valueOf(senserdata[i1]);
								 moisture.setDevice_id(device_id);
								 moisture.setValue(value);
								 MoistureDao.insert(moisture);
								 System.out.println("register_address:"+register_address+"  "+"device_id: "+device_id+"  "+"senserdata: "+senserdata[i1]);
							 }
							 }
						 }
					 }
					}
					}
				
				if (sensedevice6.size() != 0){
					for(int i = 0; i<model.size(); i++){
					int	modeladdress = model.get(i).getModel_address();
					int	modelid = Integer.parseInt( model.get(i).getId());
					 ArrayList<SenseDevice> M_sensedevice6 = senseDeviceDao.Read_Register_address(modelid, sensedevice6);//��Ӧ�ڵ��µ�����Do���͵ļĴ���
					if (M_sensedevice6.size() != 0){
					 int mac_arr[] = mac_arr(M_sensedevice6);
					 if (consistent(mac_arr)) {
		//				 ConnectionInfor connectioninfor= new ConnectionInfor();
						 connectioninfor.setBaud_rate(hatchway.getBaud_rate());
						 connectioninfor.setData_bits(hatchway.getData_bits());
						 connectioninfor.setModel_address(modeladdress);
						 connectioninfor.setOdd_even_check(hatchway.getOdd_even_check());
						 connectioninfor.setRegister_address(mac_arr[0]);
						 connectioninfor.setRegister_number(mac_arr.length);
						 connectioninfor.setStop_bit(hatchway.getStop_bit());
						 connectioninfor.setStream_control(hatchway.getStream_control());
						 connectioninfor.setHatchway_name(hatchway.getHatchway_name());
						 re_6 C = new re_6(connectioninfor);
						 int message = C.remain1() ;
					 }else{
						 for(int j = 0; j<mac_arr.length; j++){
		//					 ConnectionInfor connectioninfor= new ConnectionInfor();
							 connectioninfor.setBaud_rate(hatchway.getBaud_rate());
							 connectioninfor.setData_bits(hatchway.getData_bits());
							 connectioninfor.setModel_address(modeladdress);
							 connectioninfor.setOdd_even_check(hatchway.getOdd_even_check());
							 connectioninfor.setRegister_address(mac_arr[j]);
							 connectioninfor.setRegister_number(1);
							 connectioninfor.setStop_bit(hatchway.getStop_bit());
							 connectioninfor.setStream_control(hatchway.getStream_control());
							 connectioninfor.setHatchway_name(hatchway.getHatchway_name());
							 re_6 C = new re_6(connectioninfor);
							 int message = C.remain1() ;
						 }
					 }
					}
					}
				}	
				
		}		
	}
	
}
//		for(int i = 0; i<model.size(); i++){
//			
//		model_address= model.get(i).getModel_address();
//		
		
		
//		register_address=Integer.parseInt(senseDevice.getRegister_address());
//		List<Equipment> list = equipmentDao.ReadComs_number(com_number);
//		address = new int[list.size()][4];// [i][0]���ںţ�[i][1]�ڵ��ַ��[i][2]�Ĵ�����ַ��[i][3]�豸����,
//		for(int i=0;i<list.size();i++){
//			Equipment e=new Equipment();
//			e=list.get(i);
//			address[i][0]=Integer.parseInt(e.getStream_control());
//			address[i][1]=e.getModel_address();
//			address[i][2]=Integer.parseInt(e.getRegister_address());
//			address[i][3]=Integer.parseInt(e.getDevice_type());	
//		}
//		rtu_classify.getDevice_type();
	
	
//	static void getDevice_type(SenseDevice senseDevice){  //���豸���ͷ���
//		device_type=Integer.parseInt(senseDevice.getDevice_type());
//		senseDeviceInfor=senseDeviceDao.SenseDevicelInformation();
//
//		}
//		
//		int AI_int6 = 0;
//		int AO_int3 = 0;
//		int DI_int1 = 0;
//		int DO_int5 = 0;
//		int[][] AI_6t = new int[256][4];
//		int[][] AO_3t = new int[256][4];
//		int[][] DI_1t = new int[256][4];
//		int[][] DO_5t = new int[256][4];
//		int i1 = 0, i3 = 0, i5 = 0, i6 = 0;
//		for (int i = 0; i < address.length; i++) {
//			switch (address[i][3]) {
//			case 1:
//
//				DI_1t[i1] = address[i].clone();
//				DO_5t[i5] = address[i].clone();
//				i1++; DI_int1++;
//				i5++; DO_int5++;
//				break;
//			case 5:
//				DI_1t[i1] = address[i].clone();
//				DO_5t[i5] = address[i].clone();
//				i1++; DI_int1++;
//				i5++; DO_int5++;
//				break;
//			case 3:
//				AO_3t[i3] = address[i].clone();
//				i3++;
//				AO_int3++;
//				break;
//			case 6:
//				AI_6t[i6] = address[i].clone();
//				i6++;
//				AI_int6++;
//				break;
//
//			}
//		}
//		int [][]DI_1 = new int[DI_int1][4];
//		int [][]AO_3 = new int[AO_int3][4];
//		int [][]DO_5 = new int[DO_int5][4];
//		int [][]AI_6 = new int[AI_int6][4];
//		System.arraycopy(DI_1t, 0, DI_1, 0, DI_int1);
//		System.arraycopy(AO_3t, 0, AO_3, 0, AO_int3);
//		System.arraycopy(DO_5t, 0, DO_5, 0, DO_int5);
//		System.arraycopy(AI_6t, 0, AI_6, 0, AI_int6);
//		
//	}
//	
//	
	
	
//}
